package com.itheima.redbaby;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.itheima.redbaby.activity.CartEmptyActivity;
import com.itheima.redbaby.activity.CartModifyActivity;
import com.itheima.redbaby.activity.CategoryActivity;
import com.itheima.redbaby.activity.HomeActivity;
import com.itheima.redbaby.activity.MoreActivity;
import com.itheima.redbaby.activity.SearchListview;
import com.itheima.redbaby.net.NetUtil;
import com.itheima.redbaby.utils.PromptManager;

public class BaseActivity extends Activity {

	// 公共信息处理
	// 退出问题处理
	// 处理底部导航（通用）
	// 访问网络

	private static List<Activity> activities;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		if (activities == null) {
			GloableParams.context = getApplicationContext();
			activities = new ArrayList<Activity>();
		}

		activities.add(this);
	}

	@Override
	public void setContentView(int res) {
		super.setContentView(res);
		init();
	}

	private void init() {
		BaseClickListener clickListener = new BaseClickListener();
		imgHome = (ImageView) findViewById(R.id.imgHome);
		imgHome.setOnClickListener(clickListener);
		imgMore = (ImageView) findViewById(R.id.imgMore);
		imgMore.setOnClickListener(clickListener);
		imgClassify = (ImageView) findViewById(R.id.imgClassify);
		imgClassify.setOnClickListener(clickListener);
		imgSearch = (ImageView) findViewById(R.id.imgSearch);
		imgSearch.setOnClickListener(clickListener);
		imgShoppingCar = (ImageView) findViewById(R.id.imgShoppingCar);
		imgShoppingCar.setOnClickListener(clickListener);
		shopCarNum = (TextView) findViewById(R.id.textShopCarNum);
		
		if(BaseActivity.this instanceof HomeActivity) {
			setSelected(1);
		} else if(BaseActivity.this instanceof MoreActivity) {
			setSelected(2);
		} else if(BaseActivity.this instanceof CategoryActivity) {
			setSelected(3);
		} else if(BaseActivity.this instanceof SearchListview) {
			setSelected(4);
		} else if(BaseActivity.this instanceof CartModifyActivity || BaseActivity.this instanceof CartEmptyActivity) {
			setSelected(5);
		} else {
			setSelected(0);
		}
		
		shopCarNum.setText("" + GloableParams.cartitemlist.size());
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		AlertDialog.Builder builder = new Builder(BaseActivity.this);
		builder//
				.setTitle(R.string.app_name)//
				.setMessage("客官不要走啊~~").setPositiveButton("残忍拒绝", new AlertDialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						exitSystem();
//						android.os.Process.killProcess(android.os.Process.myPid());
						// 多个Activity——懒人听书：没有彻底退出应用
						// 将所有用到的Activity都存起来，获取全部，干掉
						// BaseActivity——onCreated——放到容器中
					}
				})//
				.setNegativeButton("回去", null)//
				.show();

		return true;
	}

	public void exitSystem() {
		removelist = new  ArrayList<Activity>();
		for (Activity item : activities) {
			removelist.add(item);
		}
//		item.finish();
		method();
	}
	
//	boolean singleFinish = true;
	
	public void method(){
		
		if(removelist != null && removelist.size() != 0){
//		singleFinish = false;
		for (Activity item : removelist) {
			activities.remove(item);
			item.finish();
		}
		removelist.clear();
		removelist = null;
		}
//		singleFinish = true;
	}
	
	@Override
	public void onBackPressed() {
		if(BaseActivity.this instanceof HomeActivity) {
			AlertDialog.Builder builder = new Builder(BaseActivity.this);
			builder//
					.setTitle(R.string.app_name)//
					.setMessage("客官不要走啊~~").setPositiveButton("残忍拒绝", new AlertDialog.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							exitSystem();
						}
					})//
					.setNegativeButton("回去", null)//
					.show();

		} else
			super.onBackPressed();
	}
	
	/**
	 * 重写退出
	 */
	@Override
	public void finish() {
//		if(singleFinish)
			activities.remove(this);
		super.finish();
	}

	/**
	 * 底部导航监听器
	 * @author ShinHye
	 * 2014-10-9 下午4:02:53
	 */
	private class BaseClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			//点击了home图标
			case R.id.imgHome:
				if(BaseActivity.this instanceof HomeActivity) {
					return;
				}
				
				setSelected(1);
				intent=new Intent(BaseActivity.this,HomeActivity.class);
				startActivity(intent);

				break;
			//点击了更多按钮
			case R.id.imgMore:
				if(BaseActivity.this instanceof MoreActivity) {
					return;
				}
				
				setSelected(2);
				intent = new Intent(BaseActivity.this,MoreActivity.class);
				startActivity(intent);
				
				break;
			//点击了分类按钮
			case R.id.imgClassify:
				if(BaseActivity.this instanceof CategoryActivity) {
					return;
				}
				
				setSelected(3);
				intent = new Intent(BaseActivity.this,CategoryActivity.class);
				startActivity(intent);
				break;
			//点击了搜索按钮
			case R.id.imgSearch:
				if(BaseActivity.this instanceof SearchListview) {
					return;
				}
				
				setSelected(4);
				intent = new Intent(BaseActivity.this, SearchListview.class);
				startActivity(intent);
				break;
			//点击购物车按钮
			case R.id.imgShoppingCar:
				if(BaseActivity.this instanceof CartModifyActivity || BaseActivity.this instanceof CartEmptyActivity) {
					return;
				}
				
				setSelected(5);
				GloableParams.isBottomEnter=true;
				if(GloableParams.cartMap.size()==0){
					intent = new Intent(BaseActivity.this,CartEmptyActivity.class);
					startActivity(intent);
				}else{
					intent = new Intent(BaseActivity.this,CartModifyActivity.class);
					startActivity(intent);
				}
				break;
			}
		}
	}
	
	/**
	 * 设置底部导航为选择状态
	 * @param position
	 */
	private void setSelected(int position) {
		imgHome.setBackgroundResource(position == 1 ? R.drawable.bar_home_selected : R.drawable.bar_home_normal);
		imgMore.setBackgroundResource(position == 2 ? R.drawable.bar_more_selected : R.drawable.bar_more_normal);
		imgClassify.setBackgroundResource(position == 3 ? R.drawable.bar_class_selected : R.drawable.bar_class_normal);
		imgSearch.setBackgroundResource(position == 4 ? R.drawable.bar_search_selected : R.drawable.bar_search_normal);
		imgShoppingCar.setBackgroundResource(position == 5 ? R.drawable.bar_shopping_selected : R.drawable.bar_shopping_normal);
	}

	/**
	 * 立即设置购物车数量
	 * @param number
	 */
	public void showCartNumber() { //TODO -2222222-
		shopCarNum.setText("" + GloableParams.cartitemlist.size());
	}
	
	/**
	 * 异步访问网络，执行业务操作。例如：可以在onPreExcute中显示加载界面，doInBackground中处理业务，onPost中隐藏加载界面，同时处理doInBackground方法返回的数据并更新界面。
	 * @author ShinHye
	 * 2014-10-9 下午4:04:44
	 */
	public abstract class MyHttpTask extends AsyncTask<Void, Void, Object> {
		public final AsyncTask<Void, Void, Object> executeHttp() {
			
			if (NetUtil.checkNetwork(BaseActivity.this)) {
				return super.execute();
			} else {
				PromptManager.showNoNetWork(BaseActivity.this);
			}
			return null;
		
		}
	}
	
	protected Intent checkOutData = new Intent();//结算中心几个界面公用的意图 
	private ImageView imgHome;
	private ImageView imgMore;
	private ImageView imgClassify;
	private ImageView imgSearch;
	private ImageView imgShoppingCar;
	private TextView shopCarNum;
	private ArrayList<Activity> removelist;
	/**
	 * 显示组件
	 * @param 
	 */
	protected void showView(View v) {
		v.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 隐藏组件
	 * 
	 */
	protected void hideView(View v) {
		v.setVisibility(View.GONE);
	}
	
	//查找id视图
	protected View FId(int res){
		return findViewById(res);
	}
}
